#ifndef __LIST___H__
#define __LIST___H__

struct list 
{
  int num;
  struct list * next;
};

void display_debug(struct list ** node)
{
  struct list * src;
  int i;
 
  src = * node;
  //for (i = 0, src; src != NULL; i++) {
  while (src) {
    printf(" [%p:%d] ", src, src->num);
    src = src->next;
    //printf(" %p ", src);
  }
  printf("\n");
}

struct list * reverse_perfect_debug(struct list * node)
{
  struct list * src, * des;

  printf("%p:%p:%p:%p:%p\n", node, node->next, node->next->next, node->next->next->next, node->next->next->next->next);
  src = node;  // 指向第一个结点
  printf("src=node %p:%p \n", src, node);
  node = NULL; // 目前，链表node为空 (num, next 都为空)
  printf("node=NULL:%p \n\n", node);
  while (src) {
    des = src; // des 指向要翻转的结点
    printf("des=src %p:%p \n",des, src);
    src = src->next; // 指向下一个要翻转的结点
    printf("src=src->next %p:%p \n", src, src);
    des->next = node; // 链接到翻转后的链表
    printf("des->next=node %p:%p \n", des->next, node);
    node = des;  // 为链接下一个做准备
    printf("node=des %p:%p \n\n", node, des);
  }
  printf("return node : %p \n", node);

  return node;  // 返回翻转后的链表
}

void  reverse_perfect_noreturn_debug(struct list ** node)
{
  struct list * src, * des;

  printf("[%p:%p:%p]\n", node, * node, ** node);
  //printf("%p:%p:%p:%p:%p\n", node, node->next, node->next->next, node->next->next->next, node->next->next->next->next);
  src = * node;  // 指向第一个结点
  printf("src=node %p:%p \n", src, * node);
  * node = NULL; // 目前，链表node为空 (num, next 都为空)
  printf("node=NULL:%p \n\n", * node);
  while (src) {
    des = src; // des 指向要翻转的结点
    printf("des=src %p:%p \n",des, src);
    src = src->next; // 指向下一个要翻转的结点
    printf("src=src->next %p:%p \n", src, src);
    des->next = * node; // 链接到翻转后的链表
    printf("des->next=node %p:%p \n", des->next, * node);
    * node = des;  // 为链接下一个做准备
    printf("node=des %p:%p \n\n", * node, des);
  }
  printf("reverse_perfect end of node : %p \n", * node);
}

struct list * reverse_debug(struct list * node)
{
  struct list * src, * des, * tmp;

  tmp = node;
  printf("node:%p\n", node);
  while(tmp != NULL) {
    printf(" [[%p]] ", tmp);
    tmp = tmp->next;
  }
  printf("\n");
  printf("%p: %p: %p: %p: %p\n", node, node->next, node->next->next, node->next->next->next, node->next->next->next->next);
  printf("%d:%11d:%11d:%11d:%11d\n", node->num, node->next->num, node->next->next->num, node->next->next->next->num, 
	 node->next->next->next->next->num);

  src = node->next;  // 指向第二个数据节点
  printf(" src=node->next:[%p:%d] [%p:%d]\n", src, src->num, node->next, node->next->num); 
  node->next = NULL; // 将原链表置为空
  printf(" node->next=NULL [%p:%d]\n\n ", node->next, node->num); 
  
  while (src) {
    printf(" while->src=[%p:%d] \n", src,  src->num); 
    des = src;
    printf(" des=src des:[%p:%d] src:[%p:%d]\n", des, des->num, src, src->num);
    src = src->next;
    printf(" src=src->next[%p]", src);
    if (src != NULL)
      printf(":[%d]\n", src->num);
    else
      printf("[blank]\n");
    des->next = node->next; // 将当前节点插到头节点的后面
    printf(" des->next=node->next [%p:%d]%p\n", des->next, des->num, node->next);
    node->next = des;
    printf(" node->next=des [%p:%d]<=>[%p:%d %p]\n", node->next, node->next->num, des, des->num, des->next);
    printf("while once end node:[%p] \n\n", node);
  }
  
  printf("return node : [%p]\n\n", node);

  return node;
}


struct list * reverse_1(struct list * node)
{
  struct list * src, * des;

  printf("%p: %p: %p: %p: %p: %p\n", node, node->next, node->next->next, node->next->next->next, node->next->next->next->next,
	 node->next->next->next->next->next);
  src = node->next;
  printf("src = node->next  %p:%p\n\n", src, node->next);
  while (src->next != NULL) {
    des = src->next;
    printf("des = src->next  %p:%p\n", des, src->next);
    src->next = des->next;
    printf("src->next = des->next  %p:%p\n", src->next, des->next);
    des->next = node->next;
    printf("des->next = node->next  %p:%p\n", des->next, node->next);
    node->next = des;
    printf("node->next = des  %p:%p\n\n", node->next, des);
  }
  printf("reverse_1 end of node %p\n", node);

  return node;
}

struct list * reverse_2(struct list * node)
{
  struct list * src, * pnext, * prev;

  src = node->next;
  printf("src = node->next %p:%p\n", src, node->next);
  pnext = src->next;
  printf("pnext = src->next %p:%p\n", pnext, src->next);
  src->next = NULL;
  printf("src->next = NULL %p\n\n", src->next);
  while (pnext) {
    printf("pnext %p \n", pnext);
    prev = pnext->next;
    printf("prev = pnext->next %p:%p\n", prev, pnext->next);
    pnext->next = src;
    printf("pnext->next = src %p:%p\n", pnext->next, src);
    src = pnext;
    printf("src = pnext %p:%p\n", src, pnext);
    pnext = prev;
    printf("pnext = prev %p:%p\n\n", pnext, prev);
  }
  node->next = src;
  printf("node->next = src %p:%p\n\n", node->next, src);

  return node;
}

struct list * merge_debug(struct list * first, struct list * second)
{
  struct list * src;

  src = first;
  printf("src first second %p:%p:%p\n\n", src, first, second);
  while (src->next) { // 改为 src, 循环多一次，地址会跑飞，因而链不起来
    printf("while src src->next %p:%p \n", src, src->next);
    src = src->next;
    printf("src = src->next  %p \n\n", src);
  }
  printf("end whiel src %p \n", src);
  src->next = second;
  printf("src first %p:%p\n", src, first);

  return first;
}


#endif /* __LIST___H__ */
